﻿using ECERP.FrameWork.Utility;
using HtmlAgilityPack;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Fang.Data
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        HttpClient httpClient = new HttpClient();
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        HtmlAgilityPack.HtmlDocument tempdoc = new HtmlAgilityPack.HtmlDocument();
        DataTable dt = new DataTable("Table_AX");
        DataTable xiangmuDetail = new DataTable("xiangmuDetail");
        DataTable houseDetail = new DataTable("houseDetail");
        string loulanName, quShu, quWei, company, address;
        string websiteHost = "http://www.xzhouse.com.cn/";
        bool debug = false;
        string loupanFile = "", xiangmuFile = "";
        public MainWindow()
        {
            InitializeComponent();
            loupanFile = System.Configuration.ConfigurationManager.AppSettings["loupanFile"];
            xiangmuFile = System.Configuration.ConfigurationManager.AppSettings["xiangmuFile"];
            dt.Columns.Add("楼盘名称", System.Type.GetType("System.String"));
            dt.Columns.Add("区属", System.Type.GetType("System.String"));
            dt.Columns.Add("区位", System.Type.GetType("System.String"));
            dt.Columns.Add("公司名称", System.Type.GetType("System.String"));
            dt.Columns.Add("地址", System.Type.GetType("System.String"));
            dt.Columns.Add("链接", System.Type.GetType("System.String"));


            xiangmuDetail.Columns.Add("项目名称", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("预售许可证号", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("发放日期", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("包含楼盘", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("销售进度", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("剩余套数", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("规划用途", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("行政区", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("区位", System.Type.GetType("System.String"));
            xiangmuDetail.Columns.Add("链接", System.Type.GetType("System.String"));


            houseDetail.Columns.Add("行号", Type.GetType("System.Int64"));
            houseDetail.Columns.Add("项目名称", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("楼栋号", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("区位", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("区属", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("备案日期", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("房屋状态", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("单元号", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("房间号", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("房屋用途", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("建筑面积", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("分摊面积", System.Type.GetType("System.String"));
            houseDetail.Columns.Add("套内面积", System.Type.GetType("System.String"));

            httpClient.Timeout = TimeSpan.FromSeconds(10);
            httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36");
            options.ItemsSource = DicSource;
            options.SelectedIndex = 0;
            if (File.Exists(xiangmuFile))
            {
                using (var stream = File.OpenRead(xiangmuFile))
                {
                    xiangmuDetail = ExcelUtility.RenderDataTableFromExcel(stream, 0, 0, true);
                }
            }

            if (File.Exists(loupanFile))
            {
                using (var stream = File.OpenRead(loupanFile))
                {
                    dt = ExcelUtility.RenderDataTableFromExcel(stream, 0, 0, true);
                }
              var firstRow=  dt.NewRow();
                firstRow["楼盘名称"] = "全部楼盘";
                dt.Rows.InsertAt(firstRow, 0);
                var data = dt.AsDataView();
                loupanBox.ItemsSource = data;
                loupanBox.DisplayMemberPath = "楼盘名称";
                loupanBox.SelectedIndex = 0;
            }
        }

        public List<string> DicSource
        {
            get
            {
                return new List<string>
                                    {
                                        "泉山区","鼓楼区","云龙区","经济开发区","铜山区","贾汪区","新沂市","邳州市","丰县","沛县","睢宁县"
                                    };
            }
        }

        private async Task DealLouPan(string selectedQu)
        {
            string url = string.Format("http://www.xzhouse.com.cn/houseSearch.aspx?dist={0}&zone=&itemName=&Submit2={1}&page={2}", Encode(selectedQu), Encode("查询"), 1);
            var response = await httpClient.GetAsync(url);
            var htmlContent = await response.Content.ReadAsStringAsync();
            doc.LoadHtml(htmlContent);
            var weiyeNode = doc.DocumentNode.SelectSingleNode("//a[contains(text(),'尾页')]");
            int totalPage = GetTotalpage(weiyeNode);
            if (totalPage > 0)
            {
                bar.Maximum = totalPage;
                bar.Value = 0;
                for (int i = 1; i < totalPage + 1; i++)
                {
                    try
                    {


                        bar.Value = i;
                        tips.Content = "正在统计" + selectedQu + "楼盘信息,第" + i + "页,共" + totalPage + "页";
                        await Task.Delay(5);
                        string url1 = string.Format("http://www.xzhouse.com.cn/houseSearch.aspx?dist={0}&zone=&itemName=&Submit2={1}&page={2}", Encode(selectedQu), Encode("查询"), i);
                        var response1 = await httpClient.GetAsync(url1);
                        var htmlContent1 = await response1.Content.ReadAsStringAsync();
                        doc.LoadHtml(htmlContent1);
                        var Nodes = doc.DocumentNode.SelectNodes("//td[@class='bom1' and @align='left' and @height=25 and @width=140]");
                        foreach (var node in Nodes)
                        {
                            try
                            {
                                var row = dt.NewRow();
                                var quShuNode = node.NextSibling.NextSibling;
                                var quWeiNode = quShuNode.NextSibling.NextSibling;
                                var companyNode = quWeiNode.NextSibling.NextSibling;
                                var addressNode = companyNode.NextSibling.NextSibling;
                                loulanName = node.InnerText;
                                quShu = quShuNode.InnerText;
                                quWei = quWeiNode.InnerText;
                                company = companyNode.InnerText;
                                address = addressNode.InnerText;
                                row["楼盘名称"] = loulanName;
                                row["区属"] = quShu;
                                row["区位"] = quWei;
                                row["公司名称"] = company;
                                row["地址"] = address;
                                row["链接"] = node.ChildNodes[0].GetAttributeValue("href", null);

                                dt.Rows.Add(row);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }

            }

        }
        private async Task DealXiangMu(DataTable dt)
        {
            bar.Maximum = dt.Rows.Count;
            bar.Value = 0;
            foreach (DataRow item in dt.Rows)
            {
                try
                {
                    await Task.Delay(5);
                    string detailUrl = websiteHost + item["链接"].ToString();
                    var response = await httpClient.GetAsync(detailUrl);
                    var detailContent = await response.Content.ReadAsStringAsync();
                    doc.LoadHtml(detailContent);
                    var xiangMuListNodes = doc.DocumentNode.SelectNodes("//table[@align='center' and @cellpadding=0 and @cellspacing=1 and @style='table-layout:fixed;word-wrap:break-word;']");
                    if (xiangMuListNodes != null)
                    {
                        bar.Value += 1;
                        foreach (var xiangMu in xiangMuListNodes)
                        {
                            try
                            {
                                var row = xiangmuDetail.NewRow();
                                tempdoc.LoadHtml(xiangMu.InnerHtml);
                                var dataNodes = tempdoc.DocumentNode.SelectNodes("//a");
                                row["项目名称"] = dataNodes[0].InnerText + "," + dataNodes[1].InnerText;
                                tips.Content = "正在获取第" + bar.Value + "个项目，" + row["项目名称"].ToString() + " 项目信息，共" + bar.Maximum + "个项目";
                                row["预售许可证号"] = dataNodes[1].InnerText;
                                row["发放日期"] = dataNodes[2].InnerText;
                                row["包含楼盘"] = dataNodes[3].InnerText;
                                row["销售进度"] = dataNodes[4].InnerText;
                                row["剩余套数"] = dataNodes[5].InnerText;
                                row["规划用途"] = dataNodes[6].InnerText;
                                row["行政区"] = dataNodes[7].InnerText;
                                row["区位"] = dataNodes[8].InnerText;
                                row["链接"] = dataNodes[0].GetAttributeValue("href", null);
                                xiangmuDetail.Rows.Add(row);
                                if (debug)
                                {
                                    break;
                                }
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }

                    if (debug)
                    {
                        break;
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }
        private async Task<Dictionary<string, List<Tuple<string, string>>>> GetLouHaoUrl(DataTable dt)
        {
            Dictionary<string, List<Tuple<string, string>>> dic = new Dictionary<string, List<Tuple<string, string>>>(dt.Rows.Count);

            bar.Maximum = dt.Rows.Count;
            bar.Value = 0;
            foreach (DataRow item in dt.Rows)
            {
                try
                {
                    bar.Value += 1;
                    await Task.Delay(5);
                    List<Tuple<string, string>> Urllist = new List<Tuple<string, string>>();
                    string name = item["项目名称"].ToString();
                    if (!dic.ContainsKey(name))
                    {
                        dic.Add(name, Urllist);
                    }
                    else
                    {
                        Urllist = dic[name];
                    }
                    string detailUrl = websiteHost + item["链接"].ToString();
                    var response = await httpClient.GetAsync(detailUrl);
                    var detailContent = await response.Content.ReadAsStringAsync();
                    doc.LoadHtml(detailContent);
                    var xiaoshouNodes = doc.DocumentNode.SelectNodes("//td[@rowspan=2 and @align='center' and @valign='middle' and @bgcolor='#ffffff']");
                    if (xiaoshouNodes != null)
                    {
                        foreach (var xiaoshouNode in xiaoshouNodes)
                        {
                            try
                            {
                                var href = xiaoshouNode.FirstChild.GetAttributeValue("href", null);
                                var louhao = xiaoshouNode.FirstChild.FirstChild.InnerText;
                                tips.Content = "正在收集" + name + " ,楼号" + louhao + "的链接信息，" + bar.Value + "/" + bar.Maximum + "个项目";
                                Tuple<string, string> tup = new Tuple<string, string>(louhao, href);
                                Urllist.Add(tup);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
            return dic;
        }
        private int GetTotalpage(HtmlNode weiyeNode)
        {
            int totalPage = 0;
            if (weiyeNode != null)
            {
                var hrefValue = weiyeNode.GetAttributeValue("href", null);
                if (!string.IsNullOrEmpty(hrefValue))
                {
                    int startIndex = hrefValue.LastIndexOf("=");
                    if (startIndex > 0)
                    {
                        int.TryParse(hrefValue.Substring(startIndex + 1), out totalPage);
                    }
                }
            }
            return totalPage;
        }
        private string Encode(string str)
        {
            return System.Web.HttpUtility.UrlEncode(str, Encoding.GetEncoding("gb2312"));
        }
     
        int houseRows = 0;
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (loupanBox.SelectedIndex == -1)
                {
                    MessageBox.Show("想选择项目数据"); return;
                }
                loupanBox.IsEnabled = false;
                getxiangluListbtn.IsEnabled = false;
                syncBtn.IsEnabled = false;
                options.IsEnabled = false;
              
                DataRow[] Xiangmurows = null;
                if(loupanBox.SelectedIndex==0)
                {
                    Xiangmurows = xiangmuDetail.Select();
                }
                else
                {
                    DataRowView dataRow = (DataRowView)loupanBox.SelectionBoxItem;
                    Xiangmurows = xiangmuDetail.Select("项目名称 like '%" + dataRow["楼盘名称"] + "%'");
                }
                if (Xiangmurows.Length == 0)
                    return;
                MainBar.Maximum = Xiangmurows.Length + 1;
                MainTip.Content = "正在收集楼号链接...";
                foreach (DataRow item in Xiangmurows)
                {
                    MainBar.Value += 1;
                    var dtt = xiangmuDetail.Clone();
                    dtt.ImportRow(item);
                    var louHaoDic = await GetLouHaoUrl(dtt);

                    MainTip.Content = "正在收集房间号信息...";
                    int loupanCount = 0;
                    foreach (var loupan in louHaoDic)
                    {
                        loupanCount += 1;
                        try
                        {
                            var loupanName = loupan.Key;
                            int louhaoCount = 0;
                            foreach (var louhao in loupan.Value)
                            {
                                louhaoCount += 1;
                                try
                                {
                                    var xiaoshouUrl = louhao.Item2;
                                    string detailUrl = websiteHost + xiaoshouUrl;
                                    var response = await httpClient.GetAsync(detailUrl);
                                    var detailContent = await response.Content.ReadAsStringAsync();
                                    doc.LoadHtml(detailContent);
                                    var table = doc.DocumentNode.SelectNodes("//table[@width=720 and @border=0 and @align='center' and @cellpadding=0 and @cellspacing=1 and @bgcolor='#FBE1C8']")[2];
                                    doc.LoadHtml(table.OuterHtml);
                                    var houses = doc.DocumentNode.SelectNodes("//a");
                                    if (houses != null)
                                    {
                                        bar.Maximum = houses.Count;
                                        bar.Value = 0;
                                        foreach (var house in houses)
                                        {
                                            houseRows += 1;
                                            bar.Value += 1;
                                            try
                                            {
                                                var row = houseDetail.NewRow();
                                                row["项目名称"] = loupanName;
                                                row["楼栋号"] = louhao.Item1;
                                                row["行号"] = houseRows;


                                                row["区属"] = item[7].ToString();
                                                row["区位"] = item[8].ToString();

                                                var houseUrl = house.GetAttributeValue("href", null);
                                                houseUrl = websiteHost + houseUrl;
                                                await Task.Delay(5);
                                                var houseContent = await (await httpClient.GetAsync(houseUrl)).Content.ReadAsStringAsync();
                                                tempdoc.LoadHtml(houseContent);
                                                var fields = tempdoc.DocumentNode.SelectNodes("//span[@id]");
                                                if (fields != null)
                                                {
                                                    row["单元号"] = fields[0].InnerText;
                                                    row["房间号"] = fields[1].InnerText;
                                                    row["房屋用途"] = fields[2].InnerText;
                                                    row["建筑面积"] = fields[4].InnerText;
                                                    row["套内面积"] = fields[5].InnerText;
                                                    row["分摊面积"] = fields[6].InnerText;
                                                    row["房屋状态"] = fields[7].InnerText;
                                                    row["备案日期"] = fields[9].InnerText;
                                                    tips.Content = "正在收集" + loupanName + ",楼号" + louhao.Item1 + "," + row["单元号"] + "单元," + row["房间号"] + "房间," + loupanCount + "/" + louHaoDic.Count + "项目，" + louhaoCount + "/" + loupan.Value.Count + "栋楼";

                                                }
                                                houseDetail.Rows.Add(row);
                                            }
                                            catch (Exception)
                                            {
                                                continue;
                                            }
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    continue;
                                }
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
                MainBar.Value = Xiangmurows.Length + 1;
                SaveFileDialog Save = new SaveFileDialog();
                Save.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
                Save.Title = "保存文件";
                if (houseDetail.Rows.Count > 0)
                {
                    if (Save.ShowDialog().Value)
                    {
                        int maxRows = 60000, totalCount = houseDetail.Rows.Count;
                        if (totalCount > maxRows)
                        {
                            var totalPage = totalCount / (maxRows * 1.0);
                            var exportdt = houseDetail.Clone();
                            for (int i = 1; i < totalPage + 1; i++)
                            {
                                exportdt.Clear();
                                var startIndex = (i - 1) * maxRows;
                                var endIndex = i * maxRows;
                                var rows = houseDetail.Select("行号>" + startIndex + " and 行号<=" + endIndex);
                                foreach (var item in rows)
                                {
                                    exportdt.ImportRow(item);
                                }
                                string FullName = Save.FileName, fileName = FullName.Substring(FullName.LastIndexOf(@"\") + 1);

                                FullName = FullName.Replace(fileName, "第" + i + "部分" + fileName);
                                ExcelUtility.RenderDataTableToExcel(exportdt, FullName);
                            }
                        }
                        else
                        {
                            ExcelUtility.RenderDataTableToExcel(houseDetail, Save.FileName);
                        }
                        MessageBox.Show("数据导出完成");
                        MainBar.Value = 0;
                        bar.Value = 0;
                        tips.Content = "";
                        MainTip.Content = "";
                    }
                }
                else
                {
                    MessageBox.Show("无任何记录需要导出");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错，请重试" + ex.ToString());
            }
            finally
            {
                syncBtn.IsEnabled = true;

                loupanBox.IsEnabled = true;
                getxiangluListbtn.IsEnabled = true;
                options.IsEnabled = true;
            }
        }

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            syncBtn.IsEnabled = false;

            //xiangmu.IsEnabled = false;
            getxiangluListbtn.IsEnabled = false;
            options.IsEnabled = false;
            MainBar.Maximum = 2;
            MainTip.Content = "正在处理楼盘列表...";
            if (!File.Exists(loupanFile))
            {
                var selectedQu = options.SelectedValue.ToString();
                await DealLouPan(selectedQu);
                dt = dt.DefaultView.ToTable(true, "楼盘名称", "区属", "区位", "公司名称", "地址", "链接");
                ExcelUtility.RenderDataTableToExcel(dt, loupanFile);
            }
            else
            {
                using (var stream = File.OpenRead(loupanFile))
                {
                    dt = ExcelUtility.RenderDataTableFromExcel(stream, 0, 0, true);
                }
            }
            var firstRow = dt.NewRow();
            firstRow["楼盘名称"] = "全部楼盘";
            dt.Rows.InsertAt(firstRow, 0);
            loupanBox.ItemsSource = dt.AsDataView();
            loupanBox.DisplayMemberPath = "楼盘名称";
            loupanBox.SelectedIndex = 0;

            MainBar.Value = 1;
            MainTip.Content = "正在处理项目列表...";
            if (!File.Exists(xiangmuFile))
            {
                await DealXiangMu(dt);
                xiangmuDetail = xiangmuDetail.DefaultView.ToTable(true, "项目名称", "预售许可证号", "发放日期", "包含楼盘", "销售进度", "剩余套数", "规划用途", "行政区", "区位", "链接");
                ExcelUtility.RenderDataTableToExcel(xiangmuDetail, xiangmuFile);
            }
            else
            {
                using (var stream = File.OpenRead(xiangmuFile))
                {
                    xiangmuDetail = ExcelUtility.RenderDataTableFromExcel(stream, 0, 0, true);
                }
            }
            MainBar.Value = 2;
            MainTip.Content = "获取项目数据完成，合计" + xiangmuDetail.Rows.Count + "个";
            //var data = xiangmuDetail.AsDataView();
            //xiangmu.ItemsSource = data;
            //xiangmu.DisplayMemberPath = "项目名称";
            syncBtn.IsEnabled = true;
            getxiangluListbtn.IsEnabled = true;
            options.IsEnabled = true;
        }

        private void loupan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (loupan.SelectedIndex != -1 && loupan.SelectionBoxItem.ToString() != "")
            //{
            //    DataRowView dataRow = (DataRowView)loupan.SelectionBoxItem;
            //    var tempDetails = xiangmuDetail.Clone();
            //    var rows = xiangmuDetail.Select("项目名称 like '%" + dataRow["楼盘名称"] + "%'");
            //    foreach (var item in rows)
            //    {
            //        tempDetails.ImportRow(item);
            //    }
            //    xiangmu.ItemsSource = tempDetails.AsDataView();

            //    xiangmu.DisplayMemberPath = "项目名称";

            //}
        }

    }
}
